<?php

namespace app\admin\controller\product;

use app\common\controller\Backend;

class Type extends Backend
{
   

    // 当前模型
    protected $model = null;

    // 无需登陆
    protected $noNeedLogin = [];
    // 无需鉴权
    protected $noNeedRight = [];

    public function __construct(){
        parent::__construct();
        
        $this->model = model('Product.Type');
    }

    public function index(){
        // 将请求中所有的参数去除html标签，去掉两边空白
        $this->request->filter(['strip_tags','trim']);

        if($this->request->isAjax()){
            // 获取表格传来的所有数据
            list($where,$sort,$order,$offset,$limit) = $this->buildparams();

            // 表格需要两个参数返回值
            $total = $this->model
                ->where($where)
                ->order($sort,$order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort,$order)
                ->limit($offset,$limit)
                ->select();

            // var_dump(collection($list)->toArray());
            // exit;

            $result = [
                'total' => $total,
                'rows' => $list
            ];

            return json($result);
            
        }
        return $this->view->fetch();
    }

    public function add(){
        if($this->request->isPost()){
            $params = $this->request->param('row/a');

            $data = [
                'name' => $params['name'],
                'weigh' => $params['weigh'],
                'thumb' => $params['thumb']
            ];

            // 插入数据库中
            $result = $this->model->validate('common/Product/Type')->save($data);

            if($result === FALSE){
                $this->error($this->model->getError());
                exit;
            }else{
                $this->success('添加商品分类成功');
                exit;
            }
        }
        return $this->view->fetch();
    }
    
    public function edit($ids=NULL){
        // 根据id判断数据是否存在
        $row = $this->model->find($ids);
        if(!$row){
            $this->error(__('No Results were found'));
            exit;
        }
        if($this->request->isPost()){
            $params = $this->request->param('row/a');
            
            $data = [
                'id' => $ids,
                'name' => $params['name'],
                'weigh' => $params['weigh'],
                'thumb' => $params['thumb']
            ];

            // 更新
            $result = $this->model->validate('common/Product/Type')->isUpdate(true)->save($data);

            if($result === FALSE){
                $this->error($this->model->getError());
                exit;
            }else{
                // 判断是否有新图片上传
                if($data['thumb'] != $row['thumb']){
                    is_file('.'.$row['thumb']) && @unlink('.'.$row['thumb']);
                }
                $this->success('编辑成功');
                exit;
            }
        }
        $this->assign('row',$row);

        return $this->view->fetch();
    }

    public function del($ids=NULL){
        // 判断是否存在
        $ids = $ids ?: $this->request->param('ids','','trim');
        $list = $this->model->select($ids);

        if(!$list){
            $this->error(__('No Results were found'));
            exit;
        }


        // 图片去空
        $thumb = array_column($list,"thumb");

        // 去重、去空
        $thumb = array_filter($thumb);
        $thumb = array_unique($thumb);


        $result = $this->model->whereIn('id',$ids)->delete(true);

        if($result === FALSE){
            $this->error('删除失败');
            exit;
        }else{
            // 删除图片
            if(!empty($thumb)){
                foreach($thumb as $item){
                    is_file('.'.$item) && @unlink('.'.$item);
                }
            }
            $this->success('删除成功');
            exit;
        }
    }
}
